//
//  DrawLayer.m
//  ChipmunkEditor
//
//  Created by Rewolf Us on 11-2-26.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DrawLayer.h"
#import "AppController.h"
#import "GlNode.h"

@implementation DrawLayer

-(id)init{
	if((self=[super init])){
		[[AppController sharedAppcontroller] setDrawLayerDelegate:self];
	}
	return self;
}

-(void)enableDrawing{
	NSLog(@"drawEnabled");
	[[CCEventDispatcher sharedDispatcher] addMouseDelegate:self priority:0];
}

-(void)disableDrawing{
	[[CCEventDispatcher sharedDispatcher] removeMouseDelegate:self];
}

-(BOOL)ccMouseDown:(NSEvent *)event{
	firstPoint=[event locationInWindow];
	NSLog(@"x=%f y=%f",firstPoint.x,firstPoint.y);
	glNode=[GlNode node];
	[self addChild:glNode];
	return YES;
}

-(BOOL)ccMouseDragged:(NSEvent *)event{
	//start drawing
	secondPoint=[event locationInWindow];
	glNode.position=ccpMidpoint(firstPoint, secondPoint);
	glNode.radius=ccpDistance(firstPoint, secondPoint)/2;
	glNode.anchorPoint=ccp(0,0);
	return YES;
}

-(BOOL)ccMouseUp:(NSEvent *)event{
	//stop and add
	CGPoint center=ccpMidpoint(firstPoint, secondPoint);
	[[AppController sharedAppcontroller] addCircleWithX:center.x-20 Y:center.y-20 radius:ccpDistance(firstPoint, secondPoint)/2];
	return YES;
}

-(void)dealloc{
	[[AppController sharedAppcontroller] release];
	[super dealloc];
}



@end
